package show_shop;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mySql.DBConnection;
import show_shop.goods_bean;
public class shop extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		
		String login_name="root";
		String login_password="2014121167";
		String driverClass="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/test";
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		String strpwd=null;
		String strpwd_1=null;
		String strpwd_2=null;
		ArrayList<goods_bean> goods=new ArrayList<goods_bean>();
		HttpSession session=request.getSession();
		
		
		DBConnection dbcon=new DBConnection(driverClass,url,login_password,login_name);
		try
		{
			con=dbcon.getConnection();
			//System.out.println("link ok!");
			pst=con.prepareStatement("select * from goods_detail");
			rs=pst.executeQuery();
			while(rs.next())
			{
				strpwd=rs.getString("name");
				strpwd_1=rs.getString("price");
				strpwd_2=rs.getString("picture");
				goods_bean feng=new goods_bean();
				feng.setName(strpwd);
				feng.setPrice(strpwd_1);
				feng.setPicture(strpwd_2);
				goods.add(feng);
			}		
			session.setAttribute("feng_goods", goods);
			response.sendRedirect("http://localhost:8080/practise/jsp/show.jsp");

		}
		
		catch (Exception e) {
			e.getStackTrace();
		}
		out.close();
	}

}
